/* eslint-disable */
import { Button, Toast, InputItem, Dialog } from "mand-mobile";
import store from "@/tools/factory"
import { TMap } from "../../tools/Tmap";
import { config } from "@/api/config"
import * as Request from '@/api/liubike';
import tools from "@/tools/index";
import wx from '@/tools/wx'
let that = null;
let timer = null;

export default {
    name: "mobileLogin",
    data() {
        return {
            phonenumber: "13056991870",
            code: "0000",
            verifyCodeText: "获取验证码"
        };
    },
    components: {
        [Button.name]: Button,
        [InputItem.name]: InputItem,
        [Toast.name]: Toast
    },
    methods: {
        startVerify() {
            Toast.loading("登录中...");
            that.userLogin();
        },
        userLogin() {
            // 用户登录事件
            Request.userLogin({
                phonenumber: this.phonenumber,
                code: this.code,
                lat: store.get("lat") || "",
                lng: store.get("lng") || "",
                areaid: store.get("routerInfo").areaId
            }).then(res => {
                Toast.hide()
                let req = res.data
                console.log(res.headers,req,'res')
                if (req.success) {
                    // 存储登录状态到storageSync
                    store.set('gwsSalt', res.headers.s)
                    store.set('loginInfo', {
                        id: req.data.id,               // 用户id
                        identity: req.data.identity,   // 用户身份证信息
                        mileage: req.data.mileage,
                        nickName: req.data.nickName,
                        integral: req.data.integral,
                        phone: req.data.phone,
                    });
                    store.set("token", req.data.token)
                    this.$router.back(-1)
                   /* this.$router.replace({
                        path: '/userCenter'
                    })*/
                } else {
                    Dialog.alert({
                        title: '温馨提示',
                        content: req.msg,
                        confirmText: '我知道了',
                        onConfirm: () => console.log('[Dialog.alert] confirm clicked'),
                    })
                }
            }).catch(err => {
                Toast.hide()
            })
        },
        viewAgreement() {
            wx.navigateTo({
                url: `/aboutAgreement?id=4`
            });
        },
        fetchVerifyCode() { // `获取验证码`按钮
            const phoneNum = this.phonenumber; // 手机号码
            if (!phoneNum) {
                Toast.failed("请输入手机号码")
                return false
            }
            // 手机号码验证通过后获取验证信息
            Request.getCode({
                mobileNum: this.phonenumber
            }).then(res => {
                if (res.success) {
                    Toast.succeed(res.msg)
                    clearInterval(timer);
                    let sec = 60;
                    timer = setInterval(function () {
                        sec--;
                        that.verifyCodeText = `${sec}s`
                        if (sec === 0) {
                            clearInterval(timer);
                            that.verifyCodeText = '重新发送'
                        }
                    }, 1000)
                } else {
                    if (res.data && res.data.errCode == 2050009) {
                        Dialog.alert({
                            title: '温馨提示',
                            content: res.msg,
                            confirmText: '更换号码',
                            onConfirm: () => console.log('[Dialog.alert] confirm clicked'),
                        })
                    } else {
                        Dialog.alert({
                            title: '温馨提示',
                            content: res.msg,
                            confirmText: '我知道了',
                            onConfirm: () => console.log('[Dialog.alert] confirm clicked'),
                        })
                    }
                }
            }).catch(err => {
            });
        },
    },
    created() {
    },
    mounted() {
        that = this;
    },
    destroy() {
        clearInterval(timer);
    },
};
